package com.qing.picture.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qing.picture.annotation.AuthCheck;
import com.qing.picture.common.BaseResponse;
import com.qing.picture.common.ResultUtils;
import com.qing.picture.constant.UserConstant;
import com.qing.picture.exception.ErrorCode;
import com.qing.picture.exception.ThrowUtils;
import com.qing.picture.model.dto.space.*;
import com.qing.picture.model.entity.Space;
import com.qing.picture.model.entity.User;
import com.qing.picture.model.enums.SpaceLevelEnum;
import com.qing.picture.model.vo.SpaceVo;
import com.qing.picture.service.SpaceService;
import com.qing.picture.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/space")
public class SpaceController {

    @Autowired
    private SpaceService spaceService;

    @Autowired
    private UserService userService;

    @PostMapping("/add")
    public BaseResponse<Long> addSpace(@RequestBody SpaceAddRequest addRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(addRequest == null, ErrorCode.PARAMS_ERROR);

        User user = userService.getLoginUser(request);

       Long spaceId = spaceService.addUserSpace(addRequest, user);
        return ResultUtils.success(spaceId);
    }

    @PutMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateSpace(@RequestBody SpaceUpdateRequest updateRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(updateRequest == null|| updateRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);

        User user = userService.getLoginUser(request);
        boolean res = spaceService.updateUserSpace(updateRequest, user);
        ThrowUtils.throwIf(!res, ErrorCode.OPERATION_ERROR,"修改空间失败");
        return ResultUtils.success(true);
    }

    @PostMapping("/edit")
    public BaseResponse<Boolean> editSpace(@RequestBody SpaceEditRequest editRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(editRequest == null || editRequest.getId() <= 0, ErrorCode.PARAMS_ERROR);

        User user = userService.getLoginUser(request);
        boolean res = spaceService.editUserSpace(editRequest, user);
        ThrowUtils.throwIf(!res, ErrorCode.OPERATION_ERROR,"修改空间失败");
        return ResultUtils.success(true);
    }


    @GetMapping("/list/level")
    public BaseResponse<List<SpaceLevel>> listSpaceLevel() {
        List<SpaceLevel> spaceLevelList = Arrays.stream(SpaceLevelEnum.values()) // 获取所有枚举
                .map(spaceLevelEnum -> new SpaceLevel(
                        spaceLevelEnum.getValue(),
                        spaceLevelEnum.getText(),
                        spaceLevelEnum.getMaxCount(),
                        spaceLevelEnum.getMaxSize()))
                .collect(Collectors.toList());
        return ResultUtils.success(spaceLevelList);
    }

    /**
     * 分页获取空间列表（封装类）
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<SpaceVo>> listSpaceVOByPage(@RequestBody SpaceQueryRequest spaceQueryRequest,
                                                         HttpServletRequest request) {
        long current = spaceQueryRequest.getCurrent();
        long size = spaceQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        Page<Space> spacePage = spaceService.page(new Page<>(current, size),
                spaceService.getQueryWrapper(spaceQueryRequest));
        // 获取封装类
        return ResultUtils.success(spaceService.getSpaceVoPage(spacePage, request));
    }
}
